﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;

public partial class BaseData_ProductBrand : System.Web.UI.Page
{

    public static Model.BRANDSHOW modal = new Model.BRANDSHOW();


    BLL.BRANDSHOW bll = new BLL.BRANDSHOW();
    string key = "";

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        Panel1.Visible = false;
        Check.CheckPermission("200200106");

        //   SearchControl1.
        if (!IsPostBack)
        {
            AspNetPager1.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["RowCountPerPage"].ToString());
            SearchControl1.InitParam("PINPAI", "PPDM", "PPMC", "PINPAIListControl1", "rptPINPAI", "AspNetPager1");
            Bind("");
        }


        if (Request.QueryString["key"] != null)
        {
            key = Request.QueryString["key"].ToString();

            //                modal=bll.GetModel()

            string sql = " and PPDM='" + key + "' ";
            Bind(sql);



            if (!IsPostBack)
            {
                if (bll.Exists(key))
                {
                    modal = bll.GetModel(key);
                    TextBox1.Text = modal.BS_DESC1;
                    TextBox2.Text = modal.BS_DESC2;
                    TextBox3.Text = modal.BS_DESC3;
                    TextBox4.Text = modal.BS_DESC4;
                    WebEditor1.Text = modal.BS_HTML1;
                    WebEditor2.Text = modal.BS_HTML2;
                    WebEditor3.Text = modal.BS_HTML3;
                    WebEditor4.Text = modal.BS_HTML4;

                    //FileUpload1.PostedFile.FileName = modal.BS_FILE1;
                    //FileUpload2.PostedFile.FileName = modal.BS_FILE2;
                    //FileUpload3.PostedFile.FileName = modal.BS_FILE3;
                    //FileUpload4.PostedFile.FileName = modal.BS_FILE4;



                }
            }








            Panel1.Visible = true;

        }





    }
    private void Bind(string strsql)
    {
        int pageCount, rowCount;
        List<Model.PINPAI> brands = new BLL.PINPAI().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "PPDM", false, strsql, out pageCount, out rowCount);
        PINPAIListControl1.Bind(brands);
        AspNetPager1.RecordCount = rowCount;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        int pageCount, rowCount;
        if (SearchControl1.IsSearchResult)
        {
            Repeater rpt = (Repeater)PINPAIListControl1.FindControl("rptDALEI");
            if (rpt != null)
            {
                string strSql;
                SearchControl1.BindParentPageControl(rpt, AspNetPager1, SearchControl1.DataTableName, SearchControl1.PrimaryKeyColumnName, SearchControl1.StrCondition, AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out pageCount, out rowCount, out strSql);
            }
        }
        else
        {
            List<Model.PINPAI> colors = new BLL.PINPAI().GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, "PPDM", false, "", out pageCount, out rowCount);
            PINPAIListControl1.Bind(colors);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {


        //保存

        //try

        //{





        BLL.PINPAI bl = new BLL.PINPAI();

        Model.PINPAI pp = bl.GetModel(key);

        modal.BS_CODE = key;
        modal.BS_NAME = pp.PPMC;
        modal.BS_DESC1 = TextBox1.Text;
        modal.BS_DESC2 = TextBox2.Text;
        modal.BS_DESC3 = TextBox3.Text;
        modal.BS_DESC4 = TextBox4.Text;
        modal.BS_HTML1 = WebEditor1.Text;
        modal.BS_HTML2 = WebEditor2.Text;
        modal.BS_HTML3 = WebEditor3.Text;
        modal.BS_HTML4 = WebEditor4.Text;
        modal.BS_FIELD1 = "";
        modal.BS_FIELD2 = "";
        //modal.BS_FILE1 = "111";
        //modal.BS_FILE2 = "111";
        //modal.BS_FILE3 = "111";
        //modal.BS_FILE4 = "111";



        if (bll.Exists(key))
        {
            bll.Update(modal);
        }
        else
        {
            bll.Add(modal);
        }

        modal = null;


        Panel1.Visible = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        modal = null;
    }


    //上传
    protected void Button1_Click(object sender, EventArgs e)
    {


        int _success = 0;
        int _file = 0;

        for (int i = 1; i < 5; i++)
        {


            FileUpload f = new FileUpload();

            if (i == 1) { f = FileUpload1; }
            if (i == 2) { f = FileUpload2; }
            if (i == 3) { f = FileUpload3; }
            if (i == 4) { f = FileUpload4; }

            if (f.PostedFile.FileName.ToString() == "")
            {
                continue;
            }







            string filePath = f.PostedFile.FileName;


            string type = f.PostedFile.ContentType;
            string filename = key + "_file" + i;
            string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();



            string ttt = Server.MapPath("../BrandFileUpload");

            if (!Directory.Exists(ttt))
            {
                Directory.CreateDirectory(ttt);
            }


            string absolutePath = Server.MapPath("../BrandFileUpload/" + filename + filenameExtension);
            f.PostedFile.SaveAs(absolutePath);
            //s         modal.BS_FILE1 = filename + filenameExtension;

            //if (f.Value.Trim() == "")
            //{
            //    Share.Alert(Page, "请选择要上传的文件！"); return;
            //}


            Model.SYSPARAM param = new Model.SYSPARAM();
            param = new BLL.SYSPARAM().GetModel("001");



            string absolutePath1 = Server.MapPath("../BrandFileUpload/" + filename + filenameExtension);
            if (param.SP_PARAM1 == 1)
            {
                string ftpip = param.SP_PARAM13;
                string ftpuser = param.SP_PARAM14;
                string ftppwd = param.SP_PARAM15;
                //   Stream st = File1.PostedFile.InputStream;


                // Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/FileUpload/" + imgname), guige[0], guige[1]);
                try
                {
                    bool result = false;
                    FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);



                    if (!fud.IsExistsDirectory("", "BrandFileUpload", out result))
                    {
                        if (result == false)
                        {
                            Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!1"); return;
                        }
                        if (!fud.MakeDir("", "BrandFileUpload"))
                        {
                            Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!2"); return;
                        }
                    }
                    else
                    {
                        if (result == false)
                        {
                            Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!3"); return;
                        }
                    }

                    if (!fud.Upload("BrandFileUpload/", absolutePath1, filename + filenameExtension))
                    {
                        if (File.Exists(absolutePath1))
                        {
                            File.Delete(absolutePath1);
                        }
                        Share.Alert(this.Page, "上传失败"); return;
                    }
                }
                catch
                {
                    Share.Alert(this.Page, "上传失败");
                }
            }
            else
            {
                //    Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/brand/" + imgname), guige[0], guige[1]);
            }

            if (i == 1)
            {
                if (i == 1) { modal.BS_FILE1 = filename + filenameExtension; } else { modal.BS_FILE1 = ""; }
            }

            if (i == 2)
            {
                if (i == 2) { modal.BS_FILE2 = filename + filenameExtension; } else { modal.BS_FILE2 = ""; }
            }
            if (i == 3)
            {
                if (i == 3) { modal.BS_FILE3 = filename + filenameExtension; } else { modal.BS_FILE3 = ""; }
            } if (i == 4)
            {
                if (i == 4) { modal.BS_FILE4 = filename + filenameExtension; } else { modal.BS_FILE4 = ""; }

            }



        }

    }
}
